//
//  LocPathViewController.m
//  Dear
//
//  Created by n22 on 15/7/24.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
// 绘制轨迹

#import "LocPathViewController.h"
#import <BaiduMapAPI/BMapKit.h>
#import "CustomOverlayView.h"
#import "CustomOverlay.h"
#import "LocationModel.h"
@interface LocPathViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
    NSMutableArray *_dataArr;
    BMKMapView *_mapView;
}
@end

@implementation LocPathViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:@"亲爱的轨迹"];
    _dataArr = [[NSMutableArray alloc]init];
    //初始化界面
    [self bulidLayout];
    //获取位置
    [self getLoc];
    
}
#pragma mark - 初始化
- (void)bulidLayout {
    //创建地图视图
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64)];
    _mapView.zoomLevel = 19;
    [self.view addSubview:_mapView];
}


#pragma mark - 获取位置
- (void)getLoc
{
    //获取亲爱的位置
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"flag":@"1"};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpCheckLocInfor];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                NSArray *itemArr = dict[@"item"];
                for (NSDictionary *itemDict in itemArr) {
                    LocationModel *location = [[LocationModel alloc]init];
                    [location setValuesForKeysWithDictionary:itemDict];
                    [_dataArr addObject:location];
                }
                //开始绘制图像
                [self customOverlayView];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];
}

#pragma mark - 绘制
- (void)customOverlayView
{
    if (_dataArr.count > 0) {
        BMKMapPoint * temppoints = new BMKMapPoint[_dataArr.count];
        for (int i = 0 ; i < _dataArr.count ; i++) {
            LocationModel *location = _dataArr[i];
            if (i == 0) {
                //定位到其中一个
                _mapView.centerCoordinate = CLLocationCoordinate2DMake([location.latitude doubleValue],[location.longitude doubleValue]);
            }
            CLLocationCoordinate2D coor;
            coor.latitude = [location.latitude doubleValue];
            coor.longitude = [location.longitude doubleValue];
            BMKMapPoint pt = BMKMapPointForCoordinate(coor);
            temppoints[i].x = pt.x;
            temppoints[i].y = pt.y;
        }
        CustomOverlay* custom2 = [[CustomOverlay alloc] initWithPoints:temppoints count:_dataArr.count];
        [_mapView addOverlay:custom2];
        delete temppoints;
        for (int i = 0 ; i < _dataArr.count ; i++) {
            LocationModel *location = _dataArr[i];
            [self addPointAnnotation:[location.latitude doubleValue ] longitude:[location.longitude doubleValue ] address:location.address];
        }
    }else{
        CLLocationCoordinate2D coor3;
        coor3.latitude = 39.915;
        coor3.longitude = 116.504;
        BMKMapPoint pt3 = BMKMapPointForCoordinate(coor3);
        CLLocationCoordinate2D coor4;
        coor4.latitude = 40.015;
        coor4.longitude = 116.504;
        BMKMapPoint pt4 = BMKMapPointForCoordinate(coor4);
        CLLocationCoordinate2D coor5;
        coor5.latitude = 39.965;
        coor5.longitude = 116.604;
        BMKMapPoint pt5 = BMKMapPointForCoordinate(coor5);
        BMKMapPoint * temppoints2 = new BMKMapPoint[3];
        temppoints2[0].x = pt3.x;
        temppoints2[0].y = pt3.y;
        temppoints2[1].x = pt4.x;
        temppoints2[1].y = pt4.y;
        temppoints2[2].x = pt5.x;
        temppoints2[2].y = pt5.y;
        CustomOverlay* custom2 = [[CustomOverlay alloc] initWithPoints:temppoints2 count:3];
        [_mapView addOverlay:custom2];
        delete temppoints2;
        //添加标注
        _mapView.centerCoordinate = CLLocationCoordinate2DMake(39.915,116.504);
    }
}
- (void)addPointAnnotation:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude address:(NSString *)address
{
    BMKPointAnnotation  *pointAnnotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = latitude;
    coor.longitude = longitude;
    pointAnnotation.coordinate = coor;
    pointAnnotation.title = @"地点";
    pointAnnotation.subtitle = address;
    [_mapView addAnnotation:pointAnnotation];
}
#pragma mark - 协议方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[CustomOverlay class]])
    {
        CustomOverlayView* cutomView = [[CustomOverlayView alloc] initWithOverlay:overlay];
        cutomView.strokeColor = [[UIColor colorWithRed:255/255.0f green:195/255.0f blue:75/255.0f alpha:1] colorWithAlphaComponent:1];
        cutomView.lineWidth = 5.0;
        return cutomView;
    }
    return nil;
    
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //普通annotation
    NSString *AnnotationViewID = @"renameMark";
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        // 设置颜色
        annotationView.pinColor = BMKPinAnnotationColorPurple;
        // 从天上掉下效果
        annotationView.animatesDrop = YES;
        // 设置可拖拽
        annotationView.draggable = YES;
        annotationView.image = [UIImage imageNamed:@"location1"];
    }
    return annotationView;
   
}



-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
